package com.fili.gui.components.date_time;

import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 29, 2006
 * Time: 1:21:50 PM
 */
public class DateChooser extends JPanel implements ChangeListener, ActionListener
{
    private JSpinner spinner;
    private ArrayList<ChangeListener> listeners;
    private JButton btnDropDown;

    private static Image imgDropDown;
    private boolean enabled;
    private DatePopupPanel datePopupPanel;

    public boolean isEnabled()
    {
        return enabled;
    }

    public void setEnabled(boolean enabled)
    {
        this.enabled = enabled;
        spinner.setEnabled(enabled);
        btnDropDown.setEnabled(enabled);
    }

    public DateChooser()
    {
        if (imgDropDown == null)
        {
            imgDropDown = getToolkit().getImage("res/img/dropdown.png");
        }

        listeners = new ArrayList<ChangeListener>();

        setLayout(new BorderLayout());

        this.spinner = new JSpinner();
        Date d1, d2, d3;
        Calendar c = Calendar.getInstance();
        d1 = c.getTime();
        c.add(Calendar.YEAR, -100);
        d2 = c.getTime();
        c.add(Calendar.YEAR, 200);
        d3 = c.getTime();
        spinner.setModel(new SpinnerDateModel(d1, d2, d3, Calendar.YEAR));
        spinner.setEditor(new JSpinner.DateEditor(spinner, "dd/MM/yyyy"));

        add(spinner, BorderLayout.CENTER);
        spinner.addChangeListener(this);

//        btnDropDown = new JButton(new ImageIcon(imgDropDown));
        btnDropDown = new BasicArrowButton(BasicArrowButton.SOUTH);
        btnDropDown.addActionListener(this);
        btnDropDown.setMargin(new Insets(2, 2, 2, 2));
        add(btnDropDown, BorderLayout.EAST);
        datePopupPanel = new DatePopupPanel();

    }

    public void addChangeListener(ChangeListener listener)
    {
        listeners.add(listener);
    }

    public void removeChangeListener(ChangeListener listener)
    {
        listeners.remove(listener);
    }

    protected void fireChangeEvent()
    {
        for (ChangeListener listener : listeners)
            listener.stateChanged(new ChangeEvent(this));
    }

    public void stateChanged(ChangeEvent e)
    {
        fireChangeEvent();
    }

    public void actionPerformed(ActionEvent e)
    {
        if (btnDropDown.equals(e.getSource()))
        {
            Point p = spinner.getLocationOnScreen();
            p.y+=spinner.getHeight();

            if (p.y+datePopupPanel.getHeight()>getToolkit().getScreenSize().height)
                p.y = getToolkit().getScreenSize().height-datePopupPanel.getHeight();

            JPopupMenu pm = new JPopupMenu();
            pm.add(datePopupPanel);
            pm.setBorder(new LineBorder(Color.blue));

            pm.show(this,0,getHeight());
        }
    }
}
